﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Threading;
using Search;
using TripLOA.GUI.FullTextSearch;
using TripLOA.GUI.internationalization;

namespace GUI
{
    /// <summary>
    /// This page contains the results of a full-text search.
    /// </summary>
    public partial class FullTextSearch : TripLOA.GUI.internationalization.LOABasePage
    {

        #region Private methods

        #region IsQueryValid
        /// <summary>
        /// Checks if the query is valid.
        /// In addition, this method removes all leading and trailing 
        /// white-space characters from the query string.
        /// </summary>
        /// <param name="query"> The term searched by the user. </param>
        /// <returns>true if the query string is valid, false otherwise.</returns>
        private static bool IsQueryValid(ref string query)
        {
            bool valid = false;

            if (query != null)
            {
                query = query.Trim();
                if (!query.Equals(""))
                    valid = true;
            }

            return valid;
        }
        #endregion


        #region DisplayTitle
        /// <summary>
        /// Display the page title according to the term searched
        /// </summary>
        /// <param name="query"></param>
        private void DisplayTitle(string query)
        {
            searchTitle.Text = ((string)GetLocalResourceObject("searchTitle")) + " \"" + query + "\" :";
        }
        #endregion


        #region DisplaySuggestion
        /// <summary>
        /// Displays a search suggestion (if present).
        /// </summary>
        /// <param name="query">The term searched by the user.</param>
        /// <param name="suggestion">The search suggestion.</param>
        private void DisplaySuggestion(string query, string suggestion)
        {
            if (suggestion != null)
            {
                suggestion = suggestion.Trim();

                if ((suggestion != null) && (!suggestion.Equals("")) && (!suggestion.Equals(query)))
                {
                    searchSuggestion.Visible = true;
                    searchSuggestionLink.Text = suggestion;
                }
                else
                    searchSuggestion.Visible = false;
            }
            else
                searchSuggestion.Visible = false;
        }
        #endregion


        #region DisplayResults
        /// <summary>
        /// If some results was found they will be shown (along with paging controls),
        /// otherwise will appear a message to inform the user that no results was found.
        /// </summary>
        /// <param name="resList">The results list</param>
        /// <param name="resPerPage">The results list</param>
        private void DisplayResults(List<FullTextResult> resList)
        {
            if (resList != null && resList.Count != 0)
            {
                // Inserts the search results in the ListView control and makes it visible
                listViewResults.DataSource = resList;
                listViewResults.Visible = true;

                // Displays the DataPager control (for paging)
                dataPagerContainer.Visible = true;

                // Displays the buttons to change the number of results per page
                resultsPerPage.Visible = true;

                // Hide the message to inform the user that no results was found
                noResultsParagraph.Visible = false;
            }
            else
            {
                // Shows a message to inform the user that no results was found
                noResultsParagraph.Visible = true;

                // Hide the results list and it's related controls
                listViewResults.Visible = false;
                dataPagerContainer.Visible = false;
                resultsPerPage.Visible = false;
            }
        }
        #endregion


        #region ChangeResultsPerPage
        /// <summary>
        /// Changes the number of results displayed in each page and return to the first page
        /// </summary>
        /// <param name="resPerPage">The number of results to display in each page</param>
        private void ChangeResultsPerPage(FullTextSearchProperties.ResultsPerPageEnum resPerPage)
        {
            // change the number of results per page and return to the first page
            listViewResultsPag.SetPageProperties(0, (int)resPerPage, false);

            // Disables the selected button
            switch (resPerPage)
            {
                case FullTextSearchProperties.ResultsPerPageEnum.Option1:
                    resultsPerPageOption1.Enabled = false;
                    resultsPerPageOption2.Enabled = true;
                    resultsPerPageOption3.Enabled = true;
                    break;

                case FullTextSearchProperties.ResultsPerPageEnum.Option2:
                    resultsPerPageOption1.Enabled = true;
                    resultsPerPageOption2.Enabled = false;
                    resultsPerPageOption3.Enabled = true;
                    break;

                case FullTextSearchProperties.ResultsPerPageEnum.Option3:
                    resultsPerPageOption1.Enabled = true;
                    resultsPerPageOption2.Enabled = true;
                    resultsPerPageOption3.Enabled = false;
                    break;
            }
        }
        #endregion


        #endregion


        #region Page events

        #region Page_Load
        /// <summary>
        /// Performs the search and display the results.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            string query = null; // The term searched by the user.
            List<FullTextResult> resList = null; // Results list

            // Retrives a FullTextSearchProperties object from the session
            FullTextSearchProperties properties = (FullTextSearchProperties)Session[LOAGlobal.Global.SESSION_KEY_FTSEARCH_PROPERTIES];

            if (!IsPostBack) // new search
            {
                // Retrives what to search from the FullTextSearchProperties object
                query = properties.Query;

                // Checks if the query is valid
                bool queryValid = IsQueryValid(ref query);

                if (queryValid) // If the query is valid -> perform the search
                {
                    // Performs the full-text search
                    string suggestion = null; // search suggestion
                    resList = SearchManager.FullTextSearch(query, 0, FullTextSearchProperties.MAX_RESULTS, Thread.CurrentThread.CurrentCulture, out suggestion);

                    // Stores results list and search suggestion in the FullTextSearchProperties object
                    properties.ResultsList = resList;
                    properties.Suggestion = suggestion;

                    // Displays the title
                    DisplayTitle(query);

                    // Displays a suggestion (if present)
                    DisplaySuggestion(query, suggestion);

                    // Displays the search results
                    DisplayResults(resList);

                    // Sets the number of results per page at the default value
                    // or at the value previously chosen by the user
                    ChangeResultsPerPage(properties.ResultsPerPage);

                }
                else // If the query isn't valid -> redirection to the home page
                    Response.Redirect("Default.aspx");
            }
            else // IsPostBack == true, e.g. change of page, change of number of results displayed in each page etc.
            {
                // Retrives the results list from the FullTextSearchProperties object
                resList = properties.ResultsList;

                // Displays the search results
                DisplayResults(resList);

                // Hides the search suggestion box
                DisplaySuggestion(null, null);
            }
        }
        #endregion


        #region Page_PreRender
        protected void Page_PreRender(object sender, EventArgs e)
        {
            // Needed to bind results to the ListView
            listViewResults.DataBind();
        }
        #endregion


        #region searchSuggestionLink_Click
        /// <summary>
        /// Perform the search of the suggested term
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void searchSuggestionLink_Click(object sender, EventArgs e)
        {
            // Retrives a FullTextSearchProperties object from the session
            FullTextSearchProperties properties = (FullTextSearchProperties)Session[LOAGlobal.Global.SESSION_KEY_FTSEARCH_PROPERTIES];

            // The suggestion become the new query
            properties.Query = properties.Suggestion;

            // Redirection to the search page
            Response.Redirect("/FullTextSearch.aspx");
        }
        #endregion


        #region resultsPerPageOption1_Click
        /// <summary>
        /// Changes the number of results displayed in each page as a result of the
        /// click on resultsPerPageOption1 button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void resultsPerPageOption1_Click(object sender, EventArgs e)
        {
            // changes the number of results per page and return to the first page
            ChangeResultsPerPage(FullTextSearchProperties.ResultsPerPageEnum.Option1);

            // Saves this choise in the FullTextSearchProperties object
            ((FullTextSearchProperties)Session[LOAGlobal.Global.SESSION_KEY_FTSEARCH_PROPERTIES]).ResultsPerPage = FullTextSearchProperties.ResultsPerPageEnum.Option1;
        }
        #endregion


        #region resultsPerPageOption2_Click
        /// <summary>
        /// Changes the number of results displayed in each page as a result of the
        /// click on resultsPerPageOption2 button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void resultsPerPageOption2_Click(object sender, EventArgs e)
        {
            // change the number of results per page and return to the first page
            ChangeResultsPerPage(FullTextSearchProperties.ResultsPerPageEnum.Option2);

            // Saves this choise in the FullTextSearchProperties object
            ((FullTextSearchProperties)Session[LOAGlobal.Global.SESSION_KEY_FTSEARCH_PROPERTIES]).ResultsPerPage = FullTextSearchProperties.ResultsPerPageEnum.Option2;
        }
        #endregion


        #region resultsPerPageOption3_Click
        /// <summary>
        /// Changes the number of results displayed in each page as a result of the
        /// click on resultsPerPageOption3 button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void resultsPerPageOption3_Click(object sender, EventArgs e)
        {
            // change the number of results per page and return to the first page
            ChangeResultsPerPage(FullTextSearchProperties.ResultsPerPageEnum.Option3);

            // Saves this choise in the FullTextSearchProperties object
            ((FullTextSearchProperties)Session[LOAGlobal.Global.SESSION_KEY_FTSEARCH_PROPERTIES]).ResultsPerPage = FullTextSearchProperties.ResultsPerPageEnum.Option3;
        }
        #endregion

        #endregion
    }
}
